﻿using System;
using System.Collections.Generic;
using System.Linq;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class DealerBiz
    {
        #region Properties

        private readonly IDealerGroupDao _dealerGroupDao;
        private readonly IDealerDao _dealerDao;

        #endregion

        #region Constructors

        public DealerBiz()
        {
            _dealerGroupDao = new DealerGroupDao();
            _dealerDao = new DealerDao();
        }

        public DealerBiz(IDealerGroupDao dealerGroupDao, IDealerDao dealerDao)
        {
            _dealerGroupDao = dealerGroupDao;
            _dealerDao = dealerDao;
        }

        #endregion

        #region Methods

        public List<DealerGroup> GetAllDealerGroups()
        {
            try
            {
                return _dealerGroupDao.GetAllDealerGroups();
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
            return new List<DealerGroup>();
        }

        public Dealer GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _dealerDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Dealer CreateDealer(Dealer dealer)
        {
            if (dealer == null) return null;
            if (string.IsNullOrEmpty(dealer.Code) || string.IsNullOrEmpty(dealer.Name)) return null;

            try
            {
                var addedDealer = _dealerDao.AddDealer(dealer);
                return addedDealer;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public Dealer UpdateDealer(Dealer dealer)
        {
            if (dealer == null) return null;

            try
            {
                _dealerDao.UpdateDealer(dealer);
                return dealer;
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _dealerDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _dealerDao.GetCurrentIdentity() + 1;
        }

        public object GetDealerGroupOptionsWs()
        {
            try
            {
                var groups = _dealerGroupDao.GetAllDealerGroups();//.Select(g => new { DisplayText = g.Name, Value = g.Id });
                groups.Insert(0, new DealerGroup { Id = 0, Name = "" });
                return new { Result = "OK", Options = groups.Select(g => new { DisplayText = g.Name, Value = g.Id }) };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DealerListByFilterWs(string name, int dealerGroupId, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                var dealerCount = _dealerDao.GetDealerCountByFilter(name, dealerGroupId);
                var dealers = _dealerDao.GetDealersByFilter(name, dealerGroupId, jtStartIndex, jtPageSize, jtSorting);

                //Return result to jTable
                return new { Result = "OK", Records = dealers, TotalRecordCount = dealerCount };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateDealerWs(Dealer dealer)
        {
            if (dealer == null) return null;
            if (string.IsNullOrEmpty(dealer.Code) || string.IsNullOrEmpty(dealer.Name)) return null;

            try
            {
                var addedDealer = _dealerDao.AddDealer(dealer);
                return new { Result = "OK", Record = addedDealer };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateDealerWs(Dealer dealer)
        {
            if (dealer == null) return null;

            try
            {
                _dealerDao.UpdateDealer(dealer);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteDealerWs(int dealerId)
        {
            try
            {
                _dealerDao.DeleteDealer(dealerId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers
        #endregion
    }
}
